/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *  2011-03-25 Marco Riccardi - implementation of visitor pattern
 *
 */
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.IElement;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLAttribute;
import org.mizartools.utility.xml.XMLElement;

public class Reconsider extends AuxiliaryItem implements IElement {

    public static final String NAME = "Reconsider";

    private INode parentNode;
    private Integer nr = null;

    public class TypTerm implements INode {

        private INode parentNode;
    	public Typ typ = null;
        public Term term = null;

        public TypTerm(Reconsider reconsider) {
        	this.parentNode = reconsider;
		}

		@Override
		public INode getParentNode() {
            return parentNode;
		}
        
		@Override
		public void accept(Visitor visitor) {
			if (visitor.visit(this)) {}
			visitor.endVisit(this);
		}

    }
    
    private LinkedList<Reconsider.TypTerm> typTermList = new LinkedList<Reconsider.TypTerm>(); 
    
    private Proposition proposition = null;
    private Justification justification = null;

    private Reconsider(INode parentNode){
        this.parentNode = parentNode;
    }

	@Override
    public INode getParentNode() {
        return parentNode;
    }

    public static Reconsider newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        Reconsider reconsider = new Reconsider(parentNode);
        XMLElement xmlElement = xmlElementList.poll();
        if (!xmlElement.getName().equals(NAME)){
            throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a " + NAME, xmlElementList);
        }
        for (XMLAttribute xmlAttribute : xmlElement.getXMLAttributeList()){
            if (xmlAttribute.getName().equals("nr")){
                if (reconsider.nr == null){
                    try {reconsider.nr = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
                throw new ElementParseException(ElementParseException.NOT_VALID_ATTRIBUTE_ELEMENT, "XMLElement has not a valid attribute " + xmlAttribute.getName(), xmlElementList);
        }

        if (!xmlElement.isEmpty()){
            while (true){
                xmlElement = xmlElementList.peek();
                if (xmlElement == null){
                    throw new ElementParseException(ElementParseException.UNEXPECTED_END_OF_FILE, "Unexpected end of XMLElement list");
                }
                if (xmlElement.getName().equals(NAME) && xmlElement.isEndElement()){
                    xmlElement = xmlElementList.poll();
                    break;
                }
                if (xmlElement.getName().equals("Typ")){
                	Reconsider.TypTerm typTerm = reconsider.new TypTerm(reconsider);
                	typTerm.typ = Typ.newInstance(reconsider, xmlElementList);
                    xmlElement = xmlElementList.peek();
                    if (xmlElement == null){
                        throw new ElementParseException(ElementParseException.UNEXPECTED_END_OF_FILE, "Unexpected end of XMLElement list");
                    }
                    if (!Term.isTerm(xmlElement.getName())) {
                        throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement list contains a <" + xmlElement.getName() + "> in element " + NAME + " in invalid position.", xmlElementList);
                    }
                	typTerm.term = Term.newInstance(reconsider, xmlElementList);
                	reconsider.typTermList.add(typTerm);
                } else if (Term.isTerm(xmlElement.getName())){
                    throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement list contains a <Term> in element " + NAME + " in invalid position.", xmlElementList);
                } else if (xmlElement.getName().equals("Proposition")){
                    if (reconsider.proposition == null) {
                        reconsider.proposition = Proposition.newInstance(reconsider, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Proposition> in element " + NAME, xmlElementList);
                    }
                } else if (Justification.isJustification(xmlElement.getName())){
                    if (reconsider.justification == null) {
                        reconsider.justification = Justification.newInstance(reconsider, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Justification> in element " + NAME, xmlElementList);
                    }
                } else 
                    throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "The XMLElement " + xmlElement.getName() + " is not valid in " + NAME, xmlElementList);
            }
        }

        return reconsider;
    }

    public LinkedList<XMLElement> getXMLElementList() {
        LinkedList<XMLElement> xmlElementList = new LinkedList<XMLElement>();
        XMLElement xmlElement = new XMLElement(NAME);
        if (this.nr != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("nr", this.nr);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        boolean isEmpty = (typTermList.isEmpty() &&
         proposition == null && justification == null);
        xmlElement.setEmpty(isEmpty);
        xmlElementList.add(xmlElement);
        for (TypTerm typTerm : typTermList){
            xmlElementList.addAll(typTerm.typ.getXMLElementList());
            xmlElementList.addAll(typTerm.term.getXMLElementList());
        }
        if (this.proposition != null){
            xmlElementList.addAll(this.proposition.getXMLElementList());
        }
        if (this.justification != null){
            xmlElementList.addAll(this.justification.getXMLElementList());
        }
        if (!isEmpty) { 
            xmlElement = new XMLElement(NAME);
            xmlElement.setEndElement(true);
            xmlElementList.add(xmlElement);
        }
        return xmlElementList;
    }

    public Integer getNr(){
        return nr;
    }

    public LinkedList<TypTerm> getTypTermList(){
        return typTermList;
    }

    public Proposition getProposition(){
        return proposition;
    }

    public Justification getJustification(){
        return justification;
    }

	@Override
	public void accept(Visitor visitor) {
		if (visitor.visit(this)) {
			for(TypTerm typTerm: this.typTermList) {
				typTerm.accept(visitor);
			}
		}
		visitor.endVisit(this);
	}

	@Override
	public String toString(){
		ToStringVisitor toStringVisitor = new ToStringVisitor();
		this.accept(toStringVisitor);
		return toStringVisitor.toString();
	}

}
